package com.east.sng.dao;

import com.east.sng.entity.param.ChargeParam;
import com.east.sng.entity.param.ChargeSearchParam;
import com.east.sng.entity.result.ChargeInfo;
import com.east.sng.utils.RedisCache;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2021/1/3
 * @Description:
 **/
@Repository
@CacheNamespace(implementation = RedisCache.class)
public interface ChargeDao {

    /**
     * 查询缴费全部信息
     * @param params 参数列表
     * @return 实例对象列表
     */
    List<ChargeInfo> selectCharges(@Param("params") ChargeSearchParam params);

    /**
     * 查询每个课程的总收费信息
     * @param startTime 从哪个缴费时间开始
     * @param endTime 到哪个缴费时间结束
     * @return 课程名称和总收费实例对象列表
     */
    @Select("select c.course_name courseName,SUM(cg.payment_amount) total from charge cg "+
            "left join stu on stu.stu_id = cg.stu_id "+
            "left join course c on c.course_id = stu.course_id "+
            "where cg.flag = 0 and cg.charge_time >= #{start} and cg.charge_time < #{end} "+
            "group by c.course_name")
    List<Map<String, Object>> selectChargeDatas(@Param("start") String startTime, @Param("end") String endTime);

    /**
     * 添加缴费信息
     * @param chargeParam 实例对象
     */
    @Insert("insert into charge(payer_name, payer_phone, payment_amount, stu_id, charge_time) " +
            "values (#{charge.payerName}, #{charge.payerPhone}, #{charge.paymentAmount}, #{charge.stuId},NOW())")
    void insertCharge(@Param("charge") ChargeParam chargeParam);

    /**
     * 删除或恢复缴费信息
     * @param chargeId 缴费id
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete("update charge set flag = #{flag} where charge_id = #{chargeId}")
    Integer deleteOrRecoverCharge(@Param("chargeId") Integer chargeId,@Param("flag") Integer flag);

    /**
     * 批量删除或恢复缴费信息
     * @param chargeIds 缴费id列表
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete({"<script>", "update charge set flag = #{flag} where charge_id in",
            "<foreach collection=\"list\" item=\"item\" open=\"(\" separator=\",\" close=\")\" >",
            "#{item}",
            "</foreach>",
            "</script>"})
    Integer batchDeleteOrRecoverCharges(@Param("list") List<Integer> chargeIds,@Param("flag") Integer flag);

    /**
     * 根据缴费id列表查询对应的缴费信息
     * @param chargeIds 缴费id列表
     * @return 缴费信息实例对象列表
     */
    @Select({"<script>","select cg.charge_id, cg.payer_name, cg.payer_phone, cg.payment_amount,",
            "cg.charge_time,stu.stu_name, c.course_name",
            "from charge cg left join stu ",
            "left join course c on c.course_id = stu.course_id ",
            "on stu.stu_id = cg.stu_id ",
            "where cg.flag = 0 and cg.charge_id in",
            "<foreach collection='list' item='stuId' open='(' separator=',' close=')'>",
            "#{stuId}",
            "</foreach>",
            "</script>"})
    List<ChargeInfo> selectChargeInfo(List<Integer> chargeIds);

}